package com.kdtech.analyse.news;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class WeixinNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://mp.weixin.qq.com/s\\?3rd=.*&__biz=.*&idx=.*&mid=.*&sn=.*",

	};
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}



	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title=doc.select("h1#activity-name").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h2#activity-name").text();
		}
		content=HtmlCleaner.getContentHtml(url, doc.select("div.text"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#js_content"));
		}
		date=DateUtils.matchDate(doc.select("p.activity-info,#post-date").text());
		String author=doc.select("span.text-ellipsis").text();
		if(StringUtils.isBlank(author)){
			 author=doc.select("a#post-user").text();
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static HashMap<String,String> getWeixinLinks(String html){
		HashMap<String, String> ret=new HashMap<String,String>();
		try {
			html=html.substring(html.indexOf("sogou.weixin.gzhcb(")+"sogou.weixin.gzhcb(".length());
			html=html.substring(0,html.lastIndexOf(")"));
			JSONObject fromObject=JSON.parseObject(html);
			JSONArray array=fromObject.getJSONArray("items");
			for(int i=0;i<array.size();i++){
				Object object=array.get(i);
				if(object!=null){
					String url=StringUtils.substringBetween(object.toString(), "<url><![CDATA[", "]]></url>");
					String title=StringUtils.substringBetween(object.toString(), "<title><![CDATA[", "]]></title>");
					url="http://weixin.sogou.com"+url;
					ret.put(url, title);
					System.out.println(title+"->" + url );
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		WeixinNewsAnalyse test=new WeixinNewsAnalyse();
		String url="http://weixin.sogou.com/gzhjs?cb=sogou.weixin.gzhcb&openid=oIWsFt_mBdOLV986l5A_sC9fNLVk&ext=JWYPUbr8sGomjcxVxBfruw-2kjlL17cREDMDMef_WF41kbvG_Q4hegJxF5M_XFBo&gzhArtKeyWord=&page=1&t=1446192009524";
//		String htmlCookie = CrawlHTML.getHtmlCookie("http://weixin.sogou.com/gzh?openid=oIWsFt_mBdOLV986l5A_sC9fNLVk&ext=JWYPUbr8sGomjcxVxBfruw-2kjlL17cREDMDMef_WF41kbvG_Q4hegJxF5M_XFBo", "utf8");
//		htmlCookie = htmlCookie.replace("black_passportid=1;", "");
//		htmlCookie = htmlCookie + "SUV=" + (System.currentTimeMillis()*1000)	;
//		String htmlCookie = WeixinCookie.getInstance().getNextCookie();
//		System.out.println(htmlCookie);
//		url=WeixinURL.getWeixinUrl(url);
//		System.out.println(url);
//		System.out.println(url);
////		String html=CrawlHTML.getHtml(url, "utf8", WeixinCookie.getInstance().getNextCookie());
//		String html=CrawlHTML.getHtml(url, "utf8", htmlCookie);
//		String html=CrawlHTML.getHtml(url, "utf8", "ABTEST=0|1446194076|v1; SNUID=4E899CE8E7E3C4A8D96E2CB4E7BF4ACD; IPLOC=CN3701; SUID=EDC74B6AE518920A000000005633281D; SUV=1446194076719699;");
        String html=CrawlHTML.getHtml(url, "utf8", "ABTEST=0|1446193181|v1; SNUID=5E74F8D8B3B690FDA46B294AB33FEF27; IPLOC=CN3701; SUV=1446194076719699; ");
////		System.out.println(html);
		HashMap<String, String> weixinLinks=test.getWeixinLinks(html);
		for (Entry<String, String> entry : weixinLinks.entrySet()) {
//			System.out.println(entry.getKey());
			UrlMeta meta=CrawlHTML.responseToURL(entry.getKey());
			NewsMeta parserHtml=test.parserHtml(meta);
			System.out.println(parserHtml);
		}
//		System.out.println(weixinLinks);
//		url=UrlArgumentTop.FromatUrl(url);
//		System.out.println(url);
//		System.out.println(test.isDetailPage(url));
//		UrlMeta meta=CrawlHTML.responseToURL(url);
//		NewsMeta parserHtml=test.parserHtml(meta);
//		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
